<template>
  <div class="My_Charts_Wrapper">
    <div :id="id" :class="className" class="My_Charts" :style="{height:height,width:width}" />
  </div>
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'

export default {
  name: 'MyCharts',
  components: {},
  mixins: [resize],
  props: {
    id: {
      type: String,
      default: 'myCharts'
    },
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    options: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  data() {
    return {
      chart: null,
      defaultOptions: {
        grid: { left: '3%', right: '4%', bottom: '3%', top: '30', containLabel: true },
        tooltip: {
          trigger: 'axis',
          axisPointer: { type: 'shadow' }
        }
      }
    }
  },
  computed: {},
  watch: {
    options: {
      deep: true,
      handler(val) {
        this.setOptions(Object.assign(val, this.options))
      }
    }
  },
  created() {
  },
  mounted() {
    this.$nextTick(() => {
      this.init()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    init() {
      this.chart = echarts.init(document.getElementById(this.id))
      this.setOptions()
    },
    setOptions() {
      this.chart.setOption(Object.assign(this.defaultOptions, this.options))
    }
  }
}
</script>

<style scoped lang="scss">
  .My_Charts_Wrapper {
    width: 100%;
    height: 100%;
  }
  .My_Charts {
    >div {
      margin: 0 auto;
    }
    /deep/ .charts-table {
      width: 100%;
      border: 1px solid #dfe6ec;
      text-align: center;
      border-collapse: collapse;
      >thead{
        >tr{
          >th{
            height: 40px;
            background: #F0F2F5;
            border-right: 1px solid #cecece;
            border-bottom: 1px solid #dfe6ec;
            &:last-child {
              border-right: none;
            }
          }
        }
      }
      >tbody {
        >tr{
          >td{
            height: 40px;
            border-right: 1px solid #cecece;
            border-bottom: 1px solid #dfe6ec;
            &:last-child {
              border-right: none;
            }
          }
          &:last-child {
            >td {
              border-bottom: none;
            }
          }
        }
      }
    }
  }
</style>
